﻿using System.Drawing;
using System.Linq;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Entities.Creatures;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Helpers.Behaviors
{
	/// <summary>
	/// Describes the lurker behavior.
	/// </summary>
	public class Lurker
	{
		public static void MoveToTarget(Monster caller, Point target)
		{
			var Monsters = GameManager.Instance.CreatureManager.Monsters;
			caller.Flags["hasMoved"] = false;

			caller.CalculateMoves(target);

			caller.Moves = caller.Moves.OrderBy(m => m.Value).ToDictionary(p => p.Key, p => p.Value);

			for (var i = 0; i < caller.Moves.Count(); i++)
			{
				var tempPoint = DungeonHelper.AddPoints(caller.Position,
											GameManager.Instance.ActionManager.DirectionVectors.ElementAt(
												(int)caller.Moves.ElementAt(i).Key).Value);

				tempPoint = DungeonLevel.ClampToDungeon(tempPoint, false);

				if (GameManager.Instance.DungeonLevel.IsOKToMove(tempPoint) &&
					!Monsters.Any(m => m.X == tempPoint.X && m.Y == tempPoint.Y))
				{
					caller.Flags["hasMoved"] = true;
					caller.Position = tempPoint;
					return;
				}
			}
			caller.Flags["hasMoved"] = true;

		}
	}
}
